// 查詢用戶是否存在
import $api from '@/common/api';

const checkUser = () => {

	let openid = ''
	let isTrue = false;
	uni.login({
		success: res => {
			$api.post({
				url: '/comm/getAppletUnionid'
			}, {
				code: res.code
			}).then(s => {
				//console.log(s)
				//存储用户信息
				uni.setStorage({
					key: 'wxUserId',
					data: s
				})
				if (s.code != '10012') {
					//console.log('用户存在');
					if (uni.getStorageSync('user') == '') {
						uni.setStorageSync('user', s.user);
						uni.setStorageSync('roles', s.roles);
						uni.setStorageSync('role', s.roles[0]);
					}
					isTrue = true
					openid = s.openid
					let item = {
						isTrue: isTrue,
						openid: openid
					}
					return item;
				} else {
					//console.log('用户不存在');
					isTrue = false
					openid = s.openid
					let item = {
						isTrue: isTrue,
						openid: openid
					}
					return item;
				}
			})
		}
	})


}
var isTrue = false;
function checkUserMobile(mobile) {
	
	$api.post({
		url: '/regedit/checkStatus',method:'post'
	}, {
		mobile: mobile
	}).then(s => {
		//console.log(s)
		//存储用户信息
		uni.setStorage({
			key: 'wxUserId',
			data: s
		})
		if (s.code != '10012') {
			if (uni.getStorageSync('user') == '') {
				uni.setStorageSync('user', s.user);
				uni.setStorageSync('roles', s.roles);
				uni.setStorageSync('role', s.roles[0]);
			}
			isTrue = true

		} else {
			isTrue = false
		}
	})
	return isTrue;
}

function openIdLogin(openid) {
	let url = "/sysuser/loginByOpenID";
	let params = {
		openid: openid
	}
	$api.post(url).then(s => {
		//console.log(s)
		//存储用户信息
	})
}
export {
	checkUser,
	checkUserMobile,
	openIdLogin
}
